next up previous contents index
Next: Expressions Up: Classes Previous: Methods

Properties

Classes can contain properties as part of their fields list. A property acts like a normal field, i.e. you can get or set it's value, but allows to redirect the access of the field through functions and procedures. They provide a means to assiciate an action with an assignment of or a reading from a class 'field'. This allows for e.g. checking that a value is valid when assigning, or, when reading, it allows to construct the value on the fly. Moreover, properties can be read-only or write only.

The prototype declaration of a property is as follows:


Properties

syntdiag2525

syntdiag2529

syntdiag2533

syntdiag2537

syntdiag2541

syntdiag2545

syntdiag2549

syntdiag2553

A read specifier is either the name of a field that contains the property, or the name of a method function that has the same return type as the property type. In the case of a simple type, this function must not accept an argument. A read specifier is optional, making the property write-only.

A write specifier is optional: If there is no write specifier, the property is read-only. A write specifier is either the name of a field, or the name of a method procedure that accepts as a sole argument a variable of the same type as the property.

The section (private, published in which the specified function or procedure resides is irrelevant. Usually, however, this will be a protected or private method.

Example: Given the following declaration:
listing2440
The following are valid statements:
listing2442
But the following would generate an error:
listing2444
because Z is a read-only property.

What happens in the above statements is that when a value needs to be read, the compiler inserts a call to the various getNNN methods of the object, and the result of this call is used. When an assignment is made, the compiler passes the value that must be assigned as a paramater to the various setNNN methods.

Because of this mechanism, properties cannot be passed as var arguments to a function or procedure, since there is no known address of the property (at least, not always).

If the property definition contains an index, then the read and write specifiers must be a function and a procedure. Moreover, these functions require an additional parameter : An integer parameter. This allows to read or write several properties with the same function. For this, the properties must have the same type.

The following is an example of a property with an index:
listing2448
When the compiler encounters an assignment to X, then SetCoord is called with as first parameter the index (1 in the above case) and with as a second parameter the value to be set.

Conversely, when reading the value of X, the compiler calls GetCoord and passes it index 1.

Indexes can only be integer values.

You can also have array properties. These are properties that accept an index, just as an array does. Only now the index doesn't have to be an ordinal type, but can be any type.

A read specifier for an array property is the name method function that has the same return type as the property type. The function must accept as a sole arguent a variable of the same type as the index type. For an array property, you cannot specify fields as read specifiers.

A write specifier for an array property is the name of a method procedure that accepts two arguments: The first argument has the same type as the index, and the second argument is a parameter of the same type as the property type.

As an example, see the following declaration:
listing2456
Then the following statements would be valid:
listing2458
While the following statements would generate errors:
listing2460
Because the index types are wrong.

Array properties can be declared as default properties. This means that it is not necessary to specify the property name when assigning or reading it. If, in the previous example, the definition of the items property would have been
listing2463
Then the assignment
listing2465
Would be equivalent to the following abbreviation.
listing2467
You can have only one default property per class, and descendent classes cannot redeclare the default property.


next up previous contents index
Next: Expressions Up: Classes Previous: Methods

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998